//
//  BusinessVideosViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/4.
//  Copyright © 2020 王洋. All rights reserved.
//


#import "StoreFocusSectionHeader.h"
#import "CommonMediaModel.h"
#import "BusinessVideoCell.h"

#import "CLPlayerView.h"

#import "BusinessVideosViewController.h"


@interface BusinessVideosViewController ()<UITableViewDataSource, UITableViewDelegate, CLTableViewCellDelegate>

@property (nonatomic, strong) NSMutableArray<CommonMediaModel *> *dataArray;


@property (nonatomic, strong) UITableView *tableView;

/**CLplayer*/
@property (nonatomic, weak) CLPlayerView *playerView;
/**记录Cell*/
@property (nonatomic, assign) UITableViewCell *cell;

@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, assign) NSInteger pageSize;



@end

@implementation BusinessVideosViewController

- (void)dealloc {
    
    [self.playerView pausePlay];
    [self.playerView destroyPlayer];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    [self.playerView pausePlay];
    [self.playerView destroyPlayer];

}

- (instancetype)initWithStoreId:(NSString *)storeId {
    
    self = [super init];
    
    if (self) {
        self.storeId = storeId;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self request_videoList];
}

#pragma mark -

- (void)initView {
//    self.title = NSStringFromClass(self.class);
//    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = [UIColor whiteColor];

    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView registerNib:[UINib nibWithNibName:@"BusinessVideoCell" bundle:nil] forCellReuseIdentifier:@"BusinessVideoCellID"];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return TTScreenWidth * 0.6;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BusinessVideoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BusinessVideoCellID" forIndexPath:indexPath];
    cell.delegate = self;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

//在willDisplayCell里面处理数据能优化tableview的滑动流畅性，cell将要出现的时候调用
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    BusinessVideoCell * myCell = (BusinessVideoCell *)cell;
//    myCell.model = self.dataArray[indexPath.row];
    
    [myCell fillCellWithModel:self.dataArray[indexPath.row]];
}

//cell离开tableView时调用
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    //因为复用，同一个cell可能会走多次
    if ([_cell isEqual:cell]) {
        //区分是否是播放器所在cell,销毁时将指针置空
        [_playerView destroyPlayer];
        _cell = nil;
    }
}

#pragma mark - 点击播放代理
- (void)cl_tableViewCellPlayVideoWithCell:(BusinessVideoCell *)cell{
    //记录被点击的Cell
    _cell = cell;
    //销毁播放器
    [_playerView destroyPlayer];
    CLPlayerView *playerView = [[CLPlayerView alloc] initWithFrame:CGRectMake(20, 10, TTScreenWidth - 40, TTScreenWidth*0.6 - 20)];
    _playerView = playerView;
    [cell.contentView addSubview:_playerView];
    //视频地址
    _playerView.url = [NSURL URLWithString:cell.model.url];

    [_playerView updateWithConfigure:^(CLPlayerViewConfigure *configure) {
        configure.topToolBarHiddenType = TopToolBarHiddenSmall;
//        config.smallGestureControl = YES;
    }];
    //播放
    [_playerView playVideo];
    //返回按钮点击事件回调
    [_playerView backButton:^(UIButton *button) {
        NSLog(@"返回按钮被点击");
    }];
    //播放完成回调
    WEAKSELF
    [_playerView endPlay:^{
        //销毁播放器
        [weakSelf.playerView destroyPlayer];
        weakSelf.playerView = nil;
        weakSelf.cell = nil;
        NSLog(@"播放完成");
    }];
}

- (NSMutableArray<CommonMediaModel *> *)dataArray {
    if (!_dataArray) {
        _dataArray = @[].mutableCopy;
    }
    return _dataArray;
}

- (void)request_videoList {
        
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:@(self.pageNum) forKey:@"pageNum"];
    [dic setValue:@(self.pageSize) forKey:@"pageSize"];
    
    NSMutableDictionary *t = @{}.mutableCopy;
    [t setValue:self.storeId forKey:@"sellerId"];
    [t setValue:@"1" forKey:@"type"];
    [dic setValue:t forKey:@"t"];

    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_StoreMedia];
    
    WEAKSELF
    [SVProgressHUD showWithStatus:@"正在加载"];
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        [SVProgressHUD dismiss];
        if ([data isKindOfClass:[NSDictionary class]]) {

            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                NSDictionary *resultDic = [data valueForKey:@"data"];
                if (![resultDic isKindOfClass:[NSDictionary class]]) {
                    return;
                }
                NSArray *list = [resultDic valueForKey:@"list"];

                if ([list isKindOfClass:[NSArray class]]) {
                    weakSelf.dataArray = @[].mutableCopy;
                    for (int i = 0; i < list.count; i++) {
                        NSDictionary *listDic = list[i];
                        CommonMediaModel *model = [CommonMediaModel modelWithDictionary:listDic];
                        
                        [weakSelf.dataArray addObject:model];
                    }
                }
                
                [weakSelf.tableView reloadData];
            }
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        
        [SVProgressHUD dismiss];
    }];
            
}


@end
